package com.maint.m

import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.net.wifi.p2p.WifiP2pDevice
import android.util.Log
import androidx.lifecycle.lifecycleScope

import com.google.gson.Gson
import com.maint.m.App
import com.maint.m.data.BluetoothDeviceInfo
import com.maint.m.data.GlobalData
import com.maint.m.utils.SPUtil
import com.maint.m.utils.SpKeyConstant
import com.rokid.security.phone.sdk.api.PSecuritySDK
import com.rokid.security.phone.sdk.api.bluetooth.classic.listener.IClassicBTClientListener
import com.rokid.security.phone.sdk.api.msg.listener.IMessageListener
import com.rokid.security.phone.sdk.api.wifip2p.listener.IWifiP2PClientListener
import com.rokid.security.phone.sdk.base.utils.other.defaultScope
import com.rokid.security.phone.sdk.base.utils.other.ktx.collect

import com.rokid.security.sdk.base.common.out.DeviceInfo
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

/**
 * Author: zhangshengwei
 * Date: 2025/6/24
 */
object DeviceLinkerManager {

    const val TAG =    "DeviceLinkerManager::"

    // 管理设备连接状态
    var isBtConnect: Boolean = false
    var isWifiConnect: Boolean = false


    var mWifiP2pDevice: WifiP2pDevice? = null
    var mBluetoothDevice: BluetoothDeviceInfo? = null
    var mGetGlassSystemInfoMsgTask:Job?=null
    var mGson = Gson()
    var DefaultName = "Rokid Glass3"

    var systemCallbackList = mutableListOf<() -> Unit>()
    fun initDevice(){


        CoroutineScope(Dispatchers.IO).launch {

            mWifiP2pDevice = getP2pDevice()
            mBluetoothDevice = getBlueToothDevice()
        }
        initObserver()
    }
    private fun initObserver(){



                PSecuritySDK.getClassicBlueToothClientService()?.addClientListener(mIClassicBTClientListener)
                PSecuritySDK.getWifiP2PClientService()?.addWifiP2PClientListener(mIWifiP2PClientListener2)


    }
    private fun getP2pDevice(): WifiP2pDevice? {
        val json = SPUtil.getInstance(App.instance).getString(SpKeyConstant.WifiP2pDevice_key)
        val wifiP2pDevice = Gson().fromJson(json, WifiP2pDevice::class.java)
        Log.d(TAG,"getP2pDevice"+json)
        return wifiP2pDevice
    }

    private fun getBlueToothDevice(): BluetoothDeviceInfo? {
        val address = SPUtil.getInstance(App.instance).getString(SpKeyConstant.BluetoothDevice_AD_Key)
        val name = SPUtil.getInstance(App.instance).getString(SpKeyConstant.BluetoothDevice_NAME_Key)
        val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
        if (address.isNotEmpty()){
            val device: BluetoothDevice = bluetoothAdapter.getRemoteDevice(address)
            return BluetoothDeviceInfo(name,address,device.type)
        }


        return null
    }

    fun saveP2pDevice(device: WifiP2pDevice) {
        mWifiP2pDevice = device
        var json = Gson().toJson(device)
        Log.d(TAG,"saveP2pDevice"+json)
        SPUtil.getInstance(App.instance).putString(SpKeyConstant.WifiP2pDevice_key,json)

    }

    fun savaBlueToothDevice(device: BluetoothDeviceInfo) {

        mBluetoothDevice = device
        var json = Gson().toJson(device)
        Log.d(TAG,"savaBlueToothDevice"+json+" "+device.name+" "+device.address)
        SPUtil.getInstance(App.instance).putString(SpKeyConstant.BluetoothDevice_AD_Key,device.address)
        SPUtil.getInstance(App.instance).putString(SpKeyConstant.BluetoothDevice_NAME_Key,device.name)
    }
    private val mIClassicBTClientListener = object : IClassicBTClientListener{
        override fun onDeviceFound(device: BluetoothDevice) {

        }

        override fun onScanFinished() {

        }

        override fun onConnect(success: Boolean) {
            Log.e(TAG, "BT onConnect:  $success")
            isBtConnect = success

        }

        override fun onConnectionRejected(reason: String, code: Int) {
//            TODO("Not yet implemented")
        }
    }

    private val mIWifiP2PClientListener2 = object : IWifiP2PClientListener {
        override fun onWifiP2pEnabled(enabled: Boolean) {
            Log.e(TAG, "p2p onWifiP2pEnabled:  $enabled")
            isWifiConnect = enabled
        }
        override fun onSelfDeviceAvailable(device: WifiP2pDevice){


        }

    }













}